from selenium import webdriver
import yaml
from faker import Faker
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from time import sleep

'''基类页面'''
class PageBase:

    __BASE_URL = "https://work.weixin.qq.com/wework_admin/loginpage_wx"
    __COOKIE_ADRESS = "../data/qiyeweixin_cookie.yaml"

    #设置随机数
    fake = Faker("zh_CN")
    username = fake.name()
    acctid = fake.ssn()
    mobile = fake.phone_number()
    district = fake.district()
    email = fake.postcode()

    def __init__(self, basedriver=None):
        if basedriver:
            self.driver = basedriver
        else:
            self.driver = webdriver.Chrome()
            self.driver.maximize_window()
            self.driver.implicitly_wait(3)

            self.driver.get(self.__BASE_URL)
            cookies = yaml.safe_load(open(self.__COOKIE_ADRESS))
            for i in cookies:
                self.driver.add_cookie(i)

        if not self.driver.current_url.startswith("http"):
            self.driver.get(self.__BASE_URL)

    def do_quit(self):
        self.driver.quit()

    def do_get(self):
        self.driver.get(self.__BASE_URL)

    def do_find(self, by, locate=None):
        if locate:
            self.driver.find_element(by, locate)
        else:
            self.driver.find_element(*by)

    def do_click(self, by, locate=None):
        if locate:
            self.driver.find_element(by, locate).click()
        else:
            self.driver.find_element(*by).click()

    def do_send_keys(self, value, by, locate=None):
        if locate:
            self.driver.find_element(by, locate).clear()
            self.driver.find_element(by, locate).send_keys(value)
        else:
            self.driver.find_element(*by).clear()
            self.driver.find_element(*by).send_keys(value)

    def wait_expect_element_visibility(self, locate:tuple):
            return WebDriverWait(self.driver, 10).until(
                expected_conditions.visibility_of_element_located(locate))













